<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class UserService extends REST_Controller
{
	function __construct(){
		parent::__construct();
		$this->load->model('usermodel','',TRUE);
	}
	
	function insert_put()
	{
		$user = new User();
		$user->UserName=$this->put('username');
		$result = $this-> usermodel-> insert($user);
		if($result)
		{
			$this->response($result, 200);
		}
		else
		{
			$this->response(array('error' => 'Couldn\'t insert user.'), 404);
		}
	}
	
	function update_post()
	{
		$user = new User();
		$user->Id = $this->post('Id');
		$user->UserName = $this->post('username');
		$result = $this->usermodel->update($user);
		
		if($result)
		{
			$this->response($result, 200);
		}
		else 
		{
			$this->response(array('error' => 'Couldn\'t insert user.'), 404);
		}
	}
	
	function getsAll_get()
	{
		$users = $this -> usermodel -> getsAll();

		if($users)
		{
			$this->response($users, 200); // 200 being the HTTP response code
		}
	
		else
		{
			$this->response(array('error' => 'Couldn\'t find any users!'), 404);
		}
	}
	
	function getsPage_get()
	{
		$users = $this -> usermodel -> getsPage($this->get('offset'), $this->get('length'));

		if($users)
		{
			$this->response($users, 200); // 200 being the HTTP response code
		}
	
		else
		{
			$this->response(array('error' => 'Couldn\'t find any users!'), 404);
		}
	}
	
	function getById_get()
	{
		$user = $this -> usermodel -> getById($this->get('Id'));
		
		if($user)
		{
			$this->response($user, 200); // 200 being the HTTP response code
		}
	
		else
		{
			$this->response(array('error' => 'Couldn\'t find any user!'), 404);
		}
	}
	
	
	function getAll_get1()
	{
		$con = mysql_connect('localhost', 'root', 'root');
		mysql_select_db('mstorec2c');
		$query = 'select * from user';
		$result = mysql_query($query) or die(mysql_errno());
		
		$users = array();
		$index = 1;
		while ($row = mysql_fetch_assoc($result))
		{
			$users[$index++] = $row;
		}
		mysql_close();
				
        if($users)
        {
            $this->response($users, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
	}
	
	function getById_get1()
	{
		$con = mysql_connect('localhost', 'root', 'root');
		mysql_select_db('mstorec2c');
		$query = 'select * from user where id='.$this -> get('id');
		$result = mysql_query($query) or die(mysql_errno());
		
		$users = array();
		$index = 1;
		while ($row = mysql_fetch_assoc($result))
		{
			$users[$index++] = $row;
		}
		mysql_close();
		
		if($users)
		{
			$this->response($users, 200); // 200 being the HTTP response code
		}
		
		else
		{
			$this->response(array('error' => 'Couldn\'t find any users!'), 404);
		}
			}
}